// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.nio.ByteBuffer;
import zombie.characters.skills.PerkFactory;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.packets.INetworkPacket;

public class Perk implements INetworkPacket {
    protected PerkFactory.Perk perk;
    protected byte perkIndex;

    public void set(PerkFactory.Perk _perk) {
        this.perk = _perk;
        if (this.perk == null) {
            this.perkIndex = -1;
        } else {
            this.perkIndex = (byte)this.perk.index();
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        this.perkIndex = b.get();
        if (this.perkIndex >= 0 && this.perkIndex <= PerkFactory.Perks.getMaxIndex()) {
            this.perk = PerkFactory.Perks.fromIndex(this.perkIndex);
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        b.putByte(this.perkIndex);
    }

    @Override
    public String getDescription() {
        return "\n\t" + this.getClass().getSimpleName() + " [ perk=( " + this.perkIndex + " )" + (this.perk == null ? "null" : this.perk.name) + " ]";
    }

    @Override
    public boolean isConsistent() {
        return this.perk != null;
    }

    public PerkFactory.Perk getPerk() {
        return this.perk;
    }
}
